// author: Mona Esmaeili

package sa4
import org.junit._
import org.junit.Assert._
import com.meterware.servletunit.ServletRunner
import com.meterware.httpunit.PostMethodWebRequest
import com.meterware.httpunit.GetMethodWebRequest
import com.meterware.httpunit.WebRequest
import com.meterware.servletunit.ServletUnitClient
import com.meterware.httpunit.WebResponse

import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpServletRequest

class testRoutingTemplateForm {

  //Testing the form
  val ss = new ServletRunner()
  ss.registerServlet("", classOf[SimpleServletWithForms].getName)

  @Test
  def testFormGet {
    val sc: ServletUnitClient = ss.newClient()
    val request: WebRequest = new GetMethodWebRequest("http://localhost/")
    val response: WebResponse = sc.getResponse(request)
    assertNotNull("No response received", response)
    val xml = scala.xml.XML.loadString(response.getText)
    assertTrue((xml \\ "form" \ "@action").text == "/postComment")
  }

  @Test
  def testFormSubmit {
    val sc: ServletUnitClient = ss.newClient()

    // Ask for the form -- this installs the handler
    {
      val request: WebRequest = new GetMethodWebRequest("http://localhost/aaaa")
      val response: WebResponse = sc.getResponse(request)
      assertNotNull("No response received", response)
      val xml = scala.xml.XML.loadString(response.getText)
      assertTrue((xml \\ "form" \ "@action").text == "/postComment")
    }

    // Submit the form.
    {
      val theAuthor = "George Orwell"
      val theQuote = "It was a bright cold day in April, and the clocks were striking thirteen."

      val request: WebRequest = new PostMethodWebRequest("http://localhost/postComment")
      request.setParameter("author", theAuthor)
      request.setParameter("content", theQuote)

      val response: WebResponse = sc.getResponse(request)
      assertNotNull("No response received", response)
      val xml = scala.xml.XML.loadString(response.getText)
      assertTrue((xml \\ "p").text contains (theAuthor + " submitted the following comment:"))
      assertTrue((xml \\ "blockquote").text contains theQuote)
    }
  }

  @Test
  def test404 {
    val sc: ServletUnitClient = ss.newClient()
    sc.setExceptionsThrownOnErrorStatus(false)

    val request: WebRequest = new GetMethodWebRequest("http://localhost/fjklsdjdkl")
    val response: WebResponse = sc.getResponse(request)
    assertTrue(response.getResponseCode == 404)
  }



  @Test
  def testAddRouteMethod {
    val sv: ServletUnitClient = ss.newClient()
    sv.setExceptionsThrownOnErrorStatus(false)
    val request: WebRequest = new GetMethodWebRequest("http://localhost/o")
    val response: WebResponse = sv.getResponse(request)
    assertNotNull("No response received", response)
  }




  //Testing Template

  @Test
  def testTemplate {
    val sv: ServletUnitClient = ss.newClient()
    val request: WebRequest = new GetMethodWebRequest("http://localhost/index.html")
    val response: WebResponse = sv.getResponse(request)
    val responseXML = (scala.xml.XML.loadString(response.getText) \\ "h1").text 
    assertTrue(responseXML.equals(responseXML)) 
  }

  @Test
  def testTemplate1 {
    val sv: ServletUnitClient = ss.newClient()
    val request: WebRequest = new GetMethodWebRequest("http://localhost/load.html")
    val response: WebResponse = sv.getResponse(request)
    val responseXML = (scala.xml.XML.loadString(response.getText) \\ "h1").text 
    assertTrue(responseXML.equals(responseXML)) 
  }

  
  
   @Test
    def testrouting {
    val sv: ServletUnitClient = ss.newClient()
    val request: WebRequest = new GetMethodWebRequest("http://localhost")
    val response: WebResponse = sv.getResponse(request)
    val responseXML = (scala.xml.XML.loadString(response.getText) \\ "h1").text 
    assertTrue(responseXML.equals(responseXML)) 
  }
    
   
    @Test   
    def testrouting1 {
    val sv: ServletUnitClient = ss.newClient()
    val request: WebRequest = new GetMethodWebRequest("http://localhost/form")
    val response: WebResponse = sv.getResponse(request)
    val responseXML = (scala.xml.XML.loadString(response.getText) \\ "h1").text 
    assertTrue(responseXML.equals(responseXML)) 
  }
  

    
    
   @Test   
    def testrouting3 {
    val sv: ServletUnitClient = ss.newClient()
    val request: WebRequest = new GetMethodWebRequest("http://localhost/showsession")
    val response: WebResponse = sv.getResponse(request)
    val responseXML = (scala.xml.XML.loadString(response.getText) \\ "h1").text 
    assertTrue(responseXML.equals(responseXML)) 
  }  
}
